/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package estates;

import com.sun.data.provider.impl.ObjectListDataProvider;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import com.sun.webui.jsf.component.DropDown;
import com.sun.webui.jsf.component.PanelLayout;
import com.sun.webui.jsf.component.Table;
import com.sun.webui.jsf.component.Button;
import com.sun.webui.jsf.component.TableRowGroup;
import com.sun.webui.jsf.component.TextArea;
import com.sun.webui.jsf.component.TextField;
import com.sun.webui.jsf.model.DefaultTableDataProvider;
import com.sun.webui.jsf.model.SingleSelectOptionsList;
import estates.util.ParamGenerator;
import estates.validation.IntValidator;
import estates.validation.StringLengthValidator;
import java.util.HashMap;
import javax.faces.FacesException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;


/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 *
 * @version wyszukaj.java
 * @version Created on 2011-04-10, 23:21:34
 * @author Karol
 */

public class wyszukaj extends AbstractPageBean {
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
    }

    StringLengthValidator slv = new StringLengthValidator(150);

    public StringLengthValidator getSlv() {
        return slv;
    }

    public void setSlv(StringLengthValidator slv) {
        this.slv = slv;
    }

    IntValidator iv = new IntValidator();

    public IntValidator getIv() {
        return iv;
    }

    public void setIv(IntValidator iv) {
        this.iv = iv;
    }

    

    private ObjectListDataProvider objectListDataProvider1 = new ObjectListDataProvider();

    public ObjectListDataProvider getObjectListDataProvider1() {
        if(this.getRequestBean1().isViewResults()){
            //return new ObjectListDataProvider();//this.getApplicationBean1().getFasada().wyszukajWszystkie(this.getRequestBean1().getSearchString()));
            if(this.getRequestBean1().getQueryType()==0){
                return new ObjectListDataProvider(this.getApplicationBean1().getOfertyWyszukaj(this.getRequestBean1().getSearchQuery()));
            }else{
                return new ObjectListDataProvider(this.getApplicationBean1().getOfertyPorownaj(this.getRequestBean1().getSearchQuery()));
            }
        }
        return new ObjectListDataProvider();
      //  return objectListDataProvider1;
    }

    public void setObjectListDataProvider1(ObjectListDataProvider oadp) {
        this.objectListDataProvider1 = oadp;
    }
    private Table wyniki = new Table();

    public Table getWyniki() {
        return wyniki;
    }

    public void setWyniki(Table t) {
        this.wyniki = t;
    }
    private TableRowGroup tableRowGroup1 = new TableRowGroup();

    public TableRowGroup getTableRowGroup1() {
        return tableRowGroup1;
    }

    public void setTableRowGroup1(TableRowGroup trg) {
        this.tableRowGroup1 = trg;
    }
    private Button button3 = new Button();

    public Button getButton3() {
        return button3;
    }

    public void setButton3(Button b) {
        this.button3 = b;
    }
    private DropDown s_typ_lokalu = new DropDown();

    public DropDown getS_typ_lokalu() {
        return s_typ_lokalu;
    }

    public void setS_typ_lokalu(DropDown dd) {
        this.s_typ_lokalu = dd;
    }
    private TextField s_numer_lokalu = new TextField();

    public TextField getS_numer_lokalu() {
        return s_numer_lokalu;
    }

    public void setS_numer_lokalu(TextField tf) {
        this.s_numer_lokalu = tf;
    }
    private TextField s_pietro = new TextField();

    public TextField getS_pietro() {
        return s_pietro;
    }

    public void setS_pietro(TextField tf) {
        this.s_pietro = tf;
    }
    private TextField s_liczba_lazieniek = new TextField();

    public TextField getS_liczba_lazieniek() {
        return s_liczba_lazieniek;
    }

    public void setS_liczba_lazieniek(TextField tf) {
        this.s_liczba_lazieniek = tf;
    }
    private DropDown s_rozkladowe = new DropDown();

    public DropDown getS_rozkladowe() {
        return s_rozkladowe;
    }

    public void setS_rozkladowe(DropDown dd) {
        this.s_rozkladowe = dd;
    }
    private TextField s_rok_budowy = new TextField();

    public TextField getS_rok_budowy() {
        return s_rok_budowy;
    }

    public void setS_rok_budowy(TextField tf) {
        this.s_rok_budowy = tf;
    }
    private TextField s_stan_prawny = new TextField();

    public TextField getS_stan_prawny() {
        return s_stan_prawny;
    }

    public void setS_stan_prawny(TextField tf) {
        this.s_stan_prawny = tf;
    }
    private TextField s_liczba_pieter = new TextField();

    public TextField getS_liczba_pieter() {
        return s_liczba_pieter;
    }

    public void setS_liczba_pieter(TextField tf) {
        this.s_liczba_pieter = tf;
    }
    private TextField s_liczba_pokoi = new TextField();

    public TextField getS_liczba_pokoi() {
        return s_liczba_pokoi;
    }

    public void setS_liczba_pokoi(TextField tf) {
        this.s_liczba_pokoi = tf;
    }
    private TextField s_ulica = new TextField();

    public TextField getS_ulica() {
        return s_ulica;
    }

    public void setS_ulica(TextField tf) {
        this.s_ulica = tf;
    }
    private TextField s_numer_budynku = new TextField();

    public TextField getS_numer_budynku() {
        return s_numer_budynku;
    }

    public void setS_numer_budynku(TextField tf) {
        this.s_numer_budynku = tf;
    }
    private TextField s_kod_pocztowy = new TextField();

    public TextField getS_kod_pocztowy() {
        return s_kod_pocztowy;
    }

    public void setS_kod_pocztowy(TextField tf) {
        this.s_kod_pocztowy = tf;
    }
    private TextField s_cena = new TextField();

    public TextField getS_cena() {
        return s_cena;
    }

    public void setS_cena(TextField tf) {
        this.s_cena = tf;
    }
    private TextField s_powierzchnia = new TextField();

    public TextField getS_powierzchnia() {
        return s_powierzchnia;
    }

    public void setS_powierzchnia(TextField tf) {
        this.s_powierzchnia = tf;
    }
    private TextArea s_opis = new TextArea();

    public TextArea getS_opis() {
        return s_opis;
    }

    public void setS_opis(TextArea ta) {
        this.s_opis = ta;
    }
    private TextField s_miejscowosc = new TextField();

    public TextField getS_miejscowosc() {
        return s_miejscowosc;
    }

    public void setS_miejscowosc(TextField tf) {
        this.s_miejscowosc = tf;
    }
    private TextField s_wojewodztwo = new TextField();

    public TextField getS_wojewodztwo() {
        return s_wojewodztwo;
    }

    public void setS_wojewodztwo(TextField tf) {
        this.s_wojewodztwo = tf;
    }
    private DropDown s_typ_oferty = new DropDown();

    public DropDown getS_typ_oferty() {
        return s_typ_oferty;
    }

    public void setS_typ_oferty(DropDown dd) {
        this.s_typ_oferty = dd;
    }

    /**
     * <p>Construct a new Page bean instance.</p>
     */
    public wyszukaj() {
    }

    /**
     * <p>Callback method that is called whenever a page is navigated to,
     * either directly via a URL, or indirectly via page navigation.
     * Customize this method to acquire resources that will be needed
     * for event handlers and lifecycle methods, whether or not this
     * page is performing post back processing.</p>
     * 
     * <p>Note that, if the current request is a postback, the property
     * values of the components do <strong>not</strong> represent any
     * values submitted with this request.  Instead, they represent the
     * property values that were saved for this view when it was rendered.</p>
     */
    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here
        
        // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("wyszukaj Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
        }
        
        // </editor-fold>
        // Perform application initialization that must complete
        // *after* managed components are initialized
        // TODO - add your own initialization code here
    }

    /**
     * <p>Callback method that is called after the component tree has been
     * restored, but before any event processing takes place.  This method
     * will <strong>only</strong> be called on a postback request that
     * is processing a form submit.  Customize this method to allocate
     * resources that will be required in your event handlers.</p>
     */
    @Override
    public void preprocess() {
    }

    /**
     * <p>Callback method that is called just before rendering takes place.
     * This method will <strong>only</strong> be called for the page that
     * will actually be rendered (and not, for example, on a page that
     * handled a postback and then navigated to a different page).  Customize
     * this method to allocate resources that will be required for rendering
     * this page.</p>
     */
    @Override
    public void prerender() {
    }



    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called (regardless of whether
     * or not this was the page that was actually rendered).  Customize this
     * method to release resources acquired in the <code>init()</code>,
     * <code>preprocess()</code>, or <code>prerender()</code> methods (or
     * acquired during execution of an event handler).</p>
     */
    @Override
    public void destroy() {
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected RequestBean1 getRequestBean1() {
        return (RequestBean1) getBean("RequestBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected ApplicationBean1 getApplicationBean1() {
        return (ApplicationBean1) getBean("ApplicationBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected SessionBean1 getSessionBean1() {
        return (SessionBean1) getBean("SessionBean1");
    }

    protected String spr1(TextField f, String a)
    {   String query = "";
        String tmp;
         if(f.getValue() != null){
            tmp = f.getValue().toString().trim();
            if(tmp.length()>0)query+=a+tmp+"&";
        }
        return query;
    }

    protected String spr2(DropDown f, String a)
    {   String query = "";
        String tmp;
          if(f.getSelected() != null){
            int rozk = Integer.parseInt(f.getSelected().toString());
            if(rozk>0){
                tmp = Integer.toString(rozk);
                query+=a+tmp+"&";
            }
        }
        return query;
    }

    public String button1_action() {

        HashMap<String,Object> hm = makequery();

        if(hm.size()>0){

            this.getRequestBean1().prepareQueryResult(RequestBean1.QUERY_SEARCH, hm);

        }

        return null;
    }

    private void fixTabs(String selected){
        if (selected.equals("1")) {
            this.getRequestBean1().setTabVisibility(RequestBean1.VIS_SHOWDEFAULT);
        } else if (selected.equals("2")) {
            this.getRequestBean1().setTabVisibility(RequestBean1.VIS_SHOWALL);
        }
    }

    public String button3_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        this.getRequestBean1().setOferta(button3.getText().toString());
        return "szczegoly";
      //  return null;
    }

    public String tab2_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        fixTabs(s_typ_oferty.getValue().toString());
        return null;
    }

    public void s_typ_oferty_processValueChange(ValueChangeEvent event) {
        String selected = event.getNewValue().toString();
        //tymczasowa implementacja
        fixTabs(selected);
    }

    protected HashMap<String,Object> makequery(){
        HashMap<String,Object> hm = new HashMap();

        Integer typOferty = ParamGenerator.ConvertSelection(s_typ_oferty);

        /* Budowa HashMapy tworzenia oferty */

        //pola tekstowe
        String [] txtFieldsOferta = {"wojewodztwo","miejscowosc","opis"};
        TextField [] txtComponentsOferta = {s_wojewodztwo,s_miejscowosc,s_opis};

        String [] txtFieldsBudynek = {"stan_prawny","ulica"};
        TextField [] txtComponentsBudynek = {s_stan_prawny,s_ulica};

        //pola liczbowe
        String [] intFieldsOferta = {"cena","powierzchnia"};
        TextField [] intComponentsOferta = {s_cena,s_powierzchnia};

        String [] intFieldsBudynek = {"rok_budowy","liczba_pieter","liczba_pokoi",
                               "kod_pocztowy","numer_budynku"};
        TextField [] intComponentsBudynek = {s_rok_budowy,
                                      s_liczba_pieter,s_liczba_pokoi,
                                      s_kod_pocztowy,s_numer_budynku};

        String [] intFieldsLokal = {"liczba_lazieniek",
                               "pietro","numer_lokalu"};
        TextField [] intComponentsLokal = {s_liczba_lazieniek,s_pietro,s_numer_lokalu};

        //komponenty typu "Dropdown"
        String [] selectionsLokal = {"typ_lokalu",};
        DropDown [] selComponentsLokal = {s_typ_lokalu};

        //komponenty typu "Dropdown" dla wartości Boolean
        String [] booleanLokal = {"czy_rozkladowy",};
        DropDown [] boolComponentsLokal = {s_rozkladowe};

        //parsowanie dla OFERTY
        if(typOferty>=0)hm.put("typ_oferty", typOferty);

        ParamGenerator.parseStrFields(hm, txtFieldsOferta, txtComponentsOferta, true);
        ParamGenerator.parseIntFields(hm, intFieldsOferta, intComponentsOferta, true);

        if(typOferty >= 1){//BUDYNEK
            ParamGenerator.parseStrFields(hm, txtFieldsBudynek, txtComponentsBudynek, true);
            ParamGenerator.parseIntFields(hm, intFieldsBudynek, intComponentsBudynek, true);
        }
        if(typOferty == 2 || typOferty==-1){//LOKAL
            //ParamGenerator.parseStrFields(arg1, txtFieldsLokal, txtComponentsLokal);
            ParamGenerator.parseIntFields(hm, intFieldsLokal, intComponentsLokal, true);
            ParamGenerator.parseSelection(hm, selectionsLokal, selComponentsLokal, true);

            //pole checkbox
            ParamGenerator.parseBoolFields(hm, booleanLokal, boolComponentsLokal, true);
        }

        return hm;
    }

    public String button2_action() {
        HashMap<String,Object> hm = makequery();

        if(hm.size()>0){

            this.getRequestBean1().prepareQueryResult(RequestBean1.QUERY_COMPARE, hm);
        }

        return null;
    }
    
}

